<?php
/**
 * Tag info
 *
 * @copyright	The XOOPS project http://xoopscube.org/
 * @license		http://www.fsf.org/copyleft/gpl.html GNU public license
 * @author		Taiwen Jiang (phppp or D.J.) <php_pp@hotmail.com>
 * @since		1.00
 * @version		$Id$
 * @package		module::tag
 */
if (!defined('XOOPS_ROOT_PATH')){ exit(); }

/**
 * Get item fields:
 * title
 * content
 * time
 * link
 * uid
 * uname
 * tags
 *
 * @var		array	$items	associative array of items: [modid][catid][itemid]
 *
 * @return	boolean
 * 
 */
function wordpress_tag_iteminfo(&$items)
{
	if(empty($items) || !is_array($items)){
		return false;
	}
	
	$items_id = array();
	foreach(array_keys($items) as $cat_id){
		// Some handling here to build the link upon catid
			// catid is not used in article, so just skip it
		foreach(array_keys($items[$cat_id]) as $item_id){
			// In wordpress, the item_id is "art_id"
			$items_id[] = intval($item_id);
		}
	}
	global $wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles;
	require_once XOOPS_ROOT_PATH."/modules/wordpress/wp-config.php";
	
	$sql = 	"	SELECT ID, post_title, post_date, post_author ".
			"	FROM {$wpdb->posts} ".
			"	WHERE post_status = 'publish' ".
			"		AND ID IN (".implode(", ", $items_id).")";
	if(!$lposts = $wpdb->get_results($sql)){
		return false;
	}
	
	$myts =& MyTextSanitizer::getInstance();
    $posts = array();
    $time_diff = get_settings('time_difference') * 3600;
	foreach ($lposts as $lpost) {
		$post = array();
		$post["title"] = encoding_wp2xoops($myts->htmlspecialchars($lpost->post_title));
		$m = $lpost->post_date;
		$post["time"] = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4)) - $time_diff ;
		$post["link"] = "?p=".$lpost->ID;
		$post["uid"] = $lpost->post_author;
		$posts[$lpost->ID] = $post;
	}
	$lposts = null;
	
	foreach(array_keys($items) as $cat_id){
		foreach(array_keys($items[$cat_id]) as $item_id){
			$items[$cat_id][$item_id] = $posts[$item_id];
		}
	}
}

/**
 * Remove orphan tag-item links
 *
 * @return	boolean
 * 
 */
function wordpress_tag_synchronization($mid)
{
	global $wpdb, $wp_queries, $table_prefix, $wp_db_version;
	require_once XOOPS_ROOT_PATH."/modules/wordpress/wp-config.php";
	$link_handler =& xoops_getmodulehandler("link", "tag");
        
	/* clear tag-item links */
	if($link_handler->mysql_major_version() >= 4):
    $sql =	"	DELETE FROM {$link_handler->table}".
    		"	WHERE ".
    		"		tag_modid = {$mid}".
    		"		AND ".
    		"		( tag_itemid NOT IN ".
    		"			( SELECT DISTINCT ID ".
    		"				FROM {$wpdb->posts} ".
    		"				WHERE post_status = 'publish'".
    		"			) ".
    		"		)";
    else:
    $sql = 	"	DELETE {$link_handler->table} FROM {$link_handler->table}".
    		"	LEFT JOIN {$wpdb->posts} AS aa ON {$link_handler->table}.tag_itemid = aa.ID ".
    		"	WHERE ".
    		"		tag_modid = {$mid}".
    		"		AND ".
    		"		( aa.ID IS NULL".
    		"			OR aa.post_status <> 'publish'".
    		"		)";
	endif;
    if (!$result = $link_handler->db->queryF($sql)) {
        //xoops_error($link_handler->db->error());
  	}
}
?>